#include "choosefriendsdialog.h"
#include<QHBoxLayout>
#include<QScrollArea>
#include<QScrollBar>
#include<QPainter>
#include<QPushButton>
#include"debug.h"
//////////////////
//可供选择的一个好友元素（单选框+头像+名字）
/////////////////
choosefriendItem::choosefriendItem(choosefriendsdialog*owner,const QString&userid,const QIcon &avatar, const QString &name, bool cheched)
    :userId(userid)
{
    QHBoxLayout*layout=new QHBoxLayout();
    this->setLayout(layout);
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    this->setFixedHeight(50);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    //创建复选框
    checkBox=new QCheckBox();
    checkBox->setFixedSize(25,25);
    checkBox->setChecked(cheched);
    checkBox->setStyleSheet("background-color:transparent");

    //创建头像
    avatarBtn=new QPushButton();
    avatarBtn->setFixedSize(QSize(40,40));
    avatarBtn->setStyleSheet("QPushButton{border:none}");
    avatarBtn->setIconSize(QSize(40,40));
    avatarBtn->setIcon(QIcon(avatar));
    avatarBtn->setStyleSheet("background-color:transparent");

    //创建名字
    namelabel=new QLabel();
    namelabel->setText(name);
    //把上面三个空间都加入布局管理器里面
    layout->addWidget(checkBox);
    layout->addWidget(avatarBtn);
    layout->addWidget(namelabel);

    //给复选框关联槽函数
    connect(checkBox,&QCheckBox::toggled,this,[=](bool checked){
        if(checked)
            {
            owner->addSelectedFriendItem(userid,avatar,name);
        }
        else
            {
           owner->deleteItem(userid);
            //取消勾选，删除
        }
    });
}
//完成当鼠标进入某一个好友元素时，背景发生变化，鼠标出去时，背景颜色取消，通过绘图事件完成
void choosefriendItem::paintEvent(QPaintEvent*event)
{
    (void)event;
    QPainter painter(this);
    //如果鼠标进入
    if(isHover)
    {
        //绘制成深色
        painter.fillRect(this->rect(),QColor(230,230,230));
    }
    else
         //绘制成浅色
     painter.fillRect(this->rect(),QColor(255,255,255));
}
//鼠标进入
void choosefriendItem::enterEvent(QEnterEvent *event)
{
    (void)event;
    isHover=true;
    //更新界面
    this->update();
}
//鼠标离开
void choosefriendItem::leaveEvent(QEvent *event)
{
    (void)event;
    isHover=false;
    //更新界面
    this->update();
}
//////////////////
//...里面的添加好友按钮的跳转
/////////////////
//增加单个好友选择元素
void choosefriendsdialog::addFriendItem(const QString&userid,const QIcon &avatar, const QString &name, bool checked)
{
    choosefriendItem*choosefrienditem=new  choosefriendItem(this,userid,avatar,name,checked);
    leftWidget->layout()->addWidget(choosefrienditem);
}
void choosefriendsdialog::addSelectedFriendItem(const QString&userid,const QIcon &avatar,const QString &name)
{
      choosefriendItem*choosefrienditem=new  choosefriendItem(this,userid,avatar,name,true);
      rightWidget->layout()->addWidget(choosefrienditem);
}

void choosefriendsdialog::deleteItem(const QString&userId)
{
    //逻辑上是去遍历我们里面所有的item,然后去比对userid，相等就进行删除操作
    //从后往前删除
    QVBoxLayout*vlayout=dynamic_cast<QVBoxLayout*>(rightWidget->layout());

    for(int i=vlayout->count()-1;i>=0;--i)
    {
        auto* item=vlayout->itemAt(i);
        if(item==nullptr||item->widget()==nullptr)
        {
            continue;
        }
        //将这个项目进行转换
        choosefriendItem*choosefrienditem=dynamic_cast< choosefriendItem*>(item->widget());
        if(choosefrienditem->getuserid()!=userId)
        {
          continue;
        }
        //进行删除操作
        vlayout->removeWidget(choosefrienditem);
        //下面这个函数就相当于把delete交给我们qt了
       choosefrienditem->deleteLater();
    }
//在遍历左侧表，把左侧勾选了的取消

    vlayout=dynamic_cast<QVBoxLayout*>(leftWidget->layout());

    for(int i=0;i<vlayout->count();--i)
    {
        auto* item=vlayout->itemAt(i);
        if(item==nullptr||item->widget()==nullptr)
        {
            continue;
        }
        //将这个项目进行转换
        choosefriendItem*choosefrienditem=dynamic_cast< choosefriendItem*>(item->widget());
        if(choosefrienditem->getuserid()!=userId)
        {
            continue;
        }
       //取消单选框的勾选状态
        choosefrienditem->getcheckBox()->setChecked(false);
    }
}


choosefriendsdialog::choosefriendsdialog(QWidget*parent):QDialog(parent)
{
    //设置窗口的基本属性
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowTitle("选择好友");
    this->setFixedSize(750,500);
    this->setWindowIcon(QIcon(":/resource/Image/wechat.png"));
    this->setStyleSheet("QDialog{background-color:rgb(255,255,255);}");
    //这个窗口总的有一个水平的布局管理器
    QHBoxLayout* layout=new  QHBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
     this->setLayout(layout);
     //初始化左侧窗口
     initleft(layout);
     //初始化右侧窗口
     initright(layout);

}
 //初始化左侧窗口
void choosefriendsdialog::initleft(QHBoxLayout *layout)
{

    //leftWidget是一个滚动区域
    QScrollArea* scollArea=new QScrollArea();
    scollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scollArea->setWidgetResizable(true);
    scollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");
    scollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:2px;background-color:rgb(255,255,255)}");
    //再在 scollArea里面增加widght
      leftWidget=new QWidget();
      scollArea->setWidget(leftWidget);
      leftWidget->setObjectName("leftWidget");
      leftWidget->setStyleSheet("#leftWidget{background-cplor:rgb(255,255,255)}");
      //左窗口里面是垂直布局管理器
      QVBoxLayout*vlayout=new QVBoxLayout();
      vlayout->setContentsMargins(20,0,20,0);
      vlayout->setAlignment(Qt::AlignTop);
       vlayout->setSpacing(0);
       leftWidget->setLayout(vlayout);
       //指定拉伸系数
        layout->addWidget(scollArea,1);

   //在垂直布局管理器里面增加好友元素,这里也是测试数据
#if TEST_1
       QIcon icon(":/resource/Image/useravatar1.png");
       for(int i=0;i<20;i++)
       {
           this->addFriendItem(QString::number(1000+i),icon,"张三"+QString::number(i),false);
       }
#endif



}
//初始化右侧窗口
void choosefriendsdialog::initright(QHBoxLayout * layout)
{
    //先创建一个网格布局
    QGridLayout*gLayout=new  QGridLayout();
    layout->addLayout(gLayout,1);
    gLayout->setSpacing(10);
    gLayout->setContentsMargins(20,0,20,20);

    //2.创建提示的label
    QLabel*tiplabel=new QLabel();
    tiplabel->setText("选择好友");
    tiplabel->setFixedHeight(50);
    tiplabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    tiplabel->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
    tiplabel->setStyleSheet("QLabel{font-size:16px;font-wigth:700}");

    //rightWidget是一个滚动区域
    QScrollArea* scollArea=new QScrollArea();
    scollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scollArea->setWidgetResizable(true);
    scollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");
    scollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:2px;background-color:rgb(255,255,255)}");
    //再在 scollArea里面增加widght
   rightWidget=new QWidget();
    //layout->addWidget(rightWidget);
    scollArea->setWidget(leftWidget);
    rightWidget->setObjectName("rightWidget");
    rightWidget->setStyleSheet("#rightWidget{background-cplor:rgb(255,255,255)}");
    //创建垂直布局管理器
    QVBoxLayout*vlayout=new QVBoxLayout();
    vlayout->setContentsMargins(20,0,20,0);
    vlayout->setAlignment(Qt::AlignTop);
    vlayout->setSpacing(0);
    rightWidget->setLayout(vlayout);
    //在创建ok取消按钮
    QPushButton*okBtn=new QPushButton();
    okBtn->setText("完成");
    QString style="QPushButton{color:rgb(9,237,123);background-color:rgb(240,240,240);border:none;border-radius:5px}";
    style+="QPushButton:hover{background-color:rgb(220,220,220)}QPushButton:pressed{background-color:rgb(200,200,200)}";
    okBtn->setStyleSheet("style");
    okBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    okBtn->setFixedHeight(50);



    QPushButton*cancelBtn=new QPushButton();
    cancelBtn->setText("取消");
    cancelBtn->setStyleSheet("style");
   cancelBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    cancelBtn->setFixedHeight(50);

    //在布局管理器中加入上面的这些元素
      gLayout->addWidget(tiplabel,0,0,1,9);
     gLayout->addWidget(scollArea,1,0,1,9);
       gLayout->addWidget(okBtn,2,1,1,3);
      gLayout->addWidget(cancelBtn,2,5,1,3);


#if 0
//这一串代码应该通过勾选左侧界面自动选择
      QIcon icon(":/resource/Image/useravatar1.png");
      for(int i=0;i<20;i++)
      {
          this->addSelectedFriendItem(QString::number(1000+i),icon,"张三"+QString::number(i));
      }
#endif
}



